package cn.edu.nwpu.warehousemanagement.config;

import cn.edu.nwpu.warehousemanagement.interceptors.AdminInterceptor;
import cn.edu.nwpu.warehousemanagement.interceptors.FaviconInterceptor;
import cn.edu.nwpu.warehousemanagement.interceptors.LoginInterceptor;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/**
 * Web配置类
 *
 * @author Loadstar
 * @version 1.0
 */

@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Resource
    private LoginInterceptor loginInterceptor;
    @Resource
    private FaviconInterceptor faviconInterceptor;
    @Resource
    private AdminInterceptor adminInterceptor;

    /**
     * 注册拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        List<String> apiDocIgnorePath = getApiDocIgnorePath();

        List<String> loginIgnorePath = getLoginIgnorePath();

        registry.addInterceptor(loginInterceptor)
                .excludePathPatterns(loginIgnorePath)
                .excludePathPatterns(apiDocIgnorePath);
        registry.addInterceptor(adminInterceptor)
                        .addPathPatterns("/api/admin/**");
        registry.addInterceptor(faviconInterceptor)
                .addPathPatterns("/favicon.ico");
    }

    private static List<String> getLoginIgnorePath() {
        List<String> loginIgnorePath = new ArrayList<>();

        loginIgnorePath.add("/api/user/login");
        loginIgnorePath.add("/api/getPublicKey");
        return loginIgnorePath;
    }

    private static List<String> getApiDocIgnorePath() {
        List<String> apiDocIgnorePath = new ArrayList<>();
        // swagger
        apiDocIgnorePath.add("/swagger-resources/**");
        apiDocIgnorePath.add("/doc.html");
        apiDocIgnorePath.add("/v3/**");
        apiDocIgnorePath.add("/webjars/**");
        apiDocIgnorePath.add("/springdoc/**");
        apiDocIgnorePath.add("/static/**");
        apiDocIgnorePath.add("/templates/**");
        apiDocIgnorePath.add("/error");
        apiDocIgnorePath.add("/cipher/check");
        apiDocIgnorePath.add("/manager/login");
        apiDocIgnorePath.add("/swagger-ui.html");
        return apiDocIgnorePath;
    }

}
